#
# THIS FILE IS AUTO-GENERATED, DO NOT HAND EDIT!
#

set -eu

CONFIGURE_AC='configure.ac'


msg() { echo "autogen: ${@}" >&2; }
warning() { echo "autogen:WARNING: ${@}" >&2; }
fatal() { echo "autogen:ERROR: ${@}" >&2; exit 1; }


# scan_macro
scan_macro()
{
    local macro="${1}"
    local line=$(egrep "^[[:blank:]]*${macro}[[:blank:]]*(\(.*|)$" "${CONFIGURE_AC}")
    echo "${line}"
}


# get_args
get_args()
{
    local line="${1}"
    echo "${line}" | sed -e "s,^[[:blank:]]*[_[:alpha:]][_[:alnum:]]*[[:blank:]]*(\(.*\))[[:blank:]]*$,\1,g"
}


# unquote
unquote()
{
    echo "${@}" | sed 's,^[[:blank:]]*\[\(.*\)\][[:blank:]]*$,\1,g; s/^[[:blank:]]*//g; s/[[:blank:]]$//g'
}


# get_arg
get_arg()
{
    local n="${1}"
    shift
    local args="${@}"
    # msg "get_arg: '${n}' '${args}'"
    if test $n -gt 0; then
        local expr="^\([^,]*,\)\{${n}\}"
        # msg "expr='${expr}'"
        args=$(echo "${args}" | sed -e "s/${expr}//g")
        # msg " => '${args}'"
    fi
    echo "${args}" | sed -e 's/,.*$//g; s/^[[:blank:]]*//g; s/[[:blank:]]*$//g;'
}


proc_scan()
{
    # AC_INIT
    local ac_init=$(scan_macro AC_INIT)
    test x"${ac_init}" != x || fatal "AC_INIT not found"
    # msg "ac_init='${ac_init}'"
    local ac_init_args=$(get_args "${ac_init}")
    # msg "args:'${ac_init_args}'"
    PACKAGE=$(unquote $(get_arg 0 "${ac_init_args}"))
    VERSION=$(unquote $(get_arg 1 "${ac_init_args}"))
    if echo "${PACKAGE}" | egrep "^[_[:alpha:]][_[:alnum:]]*$" >/dev/null; then
        true
    else
        fatal "could not extract package name (${PACKAGE})"
    fi
    # AC_CONFIG_AUX_DIR
    local cfgaux=$(scan_macro AC_CONFIG_AUX_DIR)
    if test x"${cfgaux}" != x; then
        local cfgauxargs=$(get_args "${cfgaux}")
        AUXDIR=$(unquote $(get_arg 0 "${cfgauxargs}"))
    else
        AUXDIR=
    fi
    # AC_CONFIG_HEADERS
    local ac_config_headers=$(scan_macro AC_CONFIG_HEADERS)
    if [ x"${ac_config_headers}" = x ]; then
        HAS_CONFIG_HEADERS='no'
    else
        HAS_CONFIG_HEADERS='yes'
    fi
    # LT_INIT
    local lt_init=$(scan_macro LT_INIT)
    if test x"${lt_init}" = x; then
        HAS_LT_INIT='no'
    else
        HAS_LT_INIT='yes'
    fi
}


#
proc_scan
msg "bootstraping package '${PACKAGE}' '${VERSION}'"

# [fixme] check AC_CONFIG_MACRO_DIR
test -d m4|| mkdir -vp m4

if test x"${AUXDIR}" = x; then
    msg "aux dir: none"
else
    msg "aux dir: '${AUXDIR}'"
    test -d "${AUXDIR}" || mkdir -vp "${AUXDIR}"
fi

msg "aclocal..."
aclocal || fatal "aclocal failed"

if [ "${HAS_CONFIG_HEADERS}" = "yes" ]; then
    msg "autoheader..."
    autoheader || fatal "autoheader failed"
else
    msg "no config headers"
fi

if test x"${HAS_LT_INIT}" = x"yes"; then
    msg "libtoolize..."
    libtoolize || fatal "libtoolize failed"
else
    msg "libtool not used"
fi

msg "automake..."
automake --add-missing || fatal "automake failed"

msg "autoconf..."
autoconf || fatal "autoconf failed"

msg "all done!"
